package com.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gulimall.common.utils.PageUtils;
import com.gulimall.common.utils.Query;
import com.gulimall.product.dao.BrandDao;
import com.gulimall.product.dao.CategoryBrandRelationDao;
import com.gulimall.product.dao.CategoryDao;
import com.gulimall.product.entity.BrandEntity;
import com.gulimall.product.entity.CategoryBrandRelationEntity;
import com.gulimall.product.entity.CategoryEntity;
import com.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao,
        CategoryBrandRelationEntity> implements CategoryBrandRelationService {
    @Autowired
    private BrandDao brandDao;
    @Autowired
    private CategoryDao categoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String brandId = (String) params.get("brandId");
        IPage<CategoryBrandRelationEntity> page = this.page(new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));

        return new PageUtils(page);
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity brandRelation) {
        Long brandId = brandRelation.getBrandId();
        Long catelogId = brandRelation.getCatelogId();
        BrandEntity brandEntity = brandDao.selectById(brandId);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);

        brandRelation.setBrandName(brandEntity.getName());
        brandRelation.setCatelogName(categoryEntity.getName());

        save(brandRelation);
    }

    @Override
    public void matchUpdate(Long brandId, String name) {
        CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
        relationEntity.setBrandId(brandId);
        relationEntity.setBrandName(name);

        QueryWrapper<CategoryBrandRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("brand_id", brandId);
        update(relationEntity, queryWrapper);
    }

    @Override
    @Transactional
    public void updateCategory(Long parentCid, String name) {
        baseMapper.updateCategoryName(parentCid, name);
    }

    @Override
    public List<BrandEntity> getBrandsList(Long catId) {
        List<CategoryBrandRelationEntity> relationEntities = list(new QueryWrapper<CategoryBrandRelationEntity>().eq(
                "catelog_id", catId));

        List<Long> longList =
                relationEntities.stream().map(CategoryBrandRelationEntity::getBrandId).collect(Collectors.toList());

        return brandDao.selectBatchIds(longList);
    }
}
